﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CarRental.Control.User
{
    using CarRental.Database;

    public partial class UsersListGrid : System.Web.UI.UserControl
    {
        private DataTable dataSource { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.dataSource = this.GetUsersForGridView();

            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            //if (Session["UsersListGridDT"] == null)
            //{
            //    UsersGridView.DataSource = this.dataSource;
            //    UsersGridView.DataBind();
            //}

            //else
            //{
            //    UsersGridView.DataSource = Session["UsersListGridDT"] as DataTable;
            //    UsersGridView.DataBind();
            //}

            UsersGridView.DataSource = this.dataSource;
            UsersGridView.DataBind();
        }


        private DataTable GetUsersForGridView()
        {
            var users = Database.GetUsers();

            DataTable dataTable = new DataTable();

            // - Dodanie definicji kolumn -

            dataTable.Columns.Add(new DataColumn("Id", typeof(int)));
            dataTable.Columns.Add(new DataColumn("UserName", typeof(string)));
            dataTable.Columns.Add(new DataColumn("RegisterDate", typeof(DateTime)));
            dataTable.Columns.Add(new DataColumn("Gender", typeof(string)));
            dataTable.Columns.Add(new DataColumn("IsActive", typeof(bool)));

            foreach (var row in users)
            {
                DataRow dataRow = dataTable.NewRow();

                dataRow["Id"] = row.Id;
                dataRow["UserName"] = row.UserName;
                dataRow["RegisterDate"] = row.RegisterDate;
                dataRow["Gender"] = row.Gender;
                dataRow["IsActive"] = row.IsActive;

                dataTable.Rows.Add(dataRow);
            }

            return dataTable;
        }

        protected void UsersGridViewEditEventHandler(object sender, GridViewEditEventArgs e)
        {
            UsersGridView.EditIndex = e.NewEditIndex;
            BindGrid();
        }

        protected void UsersGridViewUpdateEventHandler(object sender, GridViewUpdateEventArgs e)
        {
            // - Update the values -
            GridViewRow row = UsersGridView.Rows[e.RowIndex];

            int Id = Int32.Parse(this.dataSource.Rows[row.DataItemIndex]["Id"].ToString());
            String UserName = ((TextBox)(row.Cells[1].Controls[0])).Text;
            String Gender = ((TextBox)(row.Cells[2].Controls[0])).Text;
            bool IsActive = ((CheckBox)(row.FindControl("CheckBoxIsActive"))).Checked;

            this.dataSource.Rows[row.DataItemIndex]["UserName"] = UserName;
            this.dataSource.Rows[row.DataItemIndex]["Gender"] = Gender;
            this.dataSource.Rows[row.DataItemIndex]["IsActive"] = IsActive;

            // TODO: Komunikat o zapisaniu
            Database.EditUser(Id, UserName, Gender, String.Empty, String.Empty, IsActive);

            // - Reset the edit index.
            UsersGridView.EditIndex = -1;

            BindGrid();
        }

        protected void UsersGridViewCancelEditEventHandler(object sender, GridViewCancelEditEventArgs e)
        {
            UsersGridView.EditIndex = -1;
            BindGrid();
        }

        protected void UsersGridViewDeleteEventHandler(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = UsersGridView.Rows[e.RowIndex];
            int Id = Int32.Parse(this.dataSource.Rows[row.DataItemIndex]["Id"].ToString());

            Database.DeleteUser(Id);

            UsersGridView.EditIndex = -1;

            this.dataSource = this.GetUsersForGridView();
            BindGrid();
        }

    }
}